package com.longdaji.decoration.ui.main.mine;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.longdaji.decoration.DecorationApplication;
import com.longdaji.decoration.R;
import com.longdaji.decoration.base.BaseFragment;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.ui.activitiesOfCommunity.news.NewsActivity;
import com.longdaji.decoration.ui.activitiesOfMine.allOrder.AllOrderActivity;
import com.longdaji.decoration.ui.activitiesOfMine.balance.BalanceActivity;
import com.longdaji.decoration.ui.activitiesOfMine.collection.CollectionActivity;
import com.longdaji.decoration.ui.activitiesOfMine.feedback.FeedbackActivity;
import com.longdaji.decoration.ui.activitiesOfMine.footprint.FootprintActivity;
import com.longdaji.decoration.ui.activitiesOfMine.housemaster.HousemasterActivity;
import com.longdaji.decoration.ui.activitiesOfMine.pendingDelivery.PendingDeliveryActivity;
import com.longdaji.decoration.ui.activitiesOfMine.pendingEvaluated.PendingEvaluatedActivity;
import com.longdaji.decoration.ui.activitiesOfMine.pendingPayment.PendingPaymentActivity;
import com.longdaji.decoration.ui.activitiesOfMine.pendingReceiving.PendingReceivingActivity;
import com.longdaji.decoration.ui.activitiesOfMine.refund.RefundActivity;
import com.longdaji.decoration.ui.activitiesOfMine.setting.SettingActivity;
import com.longdaji.decoration.ui.login.LoginActivity;
import com.longdaji.decoration.utils.ImageUtil;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.utils.ScreenUtils;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by longtao on 2018/1/14.
 */

public class MineFragment extends BaseFragment implements MineContract.View {
    @Inject
    public MineFragment() {
    }

    @Inject
    MineContract.Presenter mPresenter;

    @BindView(R.id.iv_user_portrait)
    ImageView ivUserPortrait;
    @BindView(R.id.tv_user_signature)
    TextView tvUserSignature;
    @BindView(R.id.iv_user_background)
    ImageView ivUserBackground;
    private Account account;
    @BindView(R.id.iv_setting)
    ImageView ivSetting;
    @BindView(R.id.iv_news)
    ImageView ivNews;
    @BindView(R.id.rl_pending_delivery)
    RelativeLayout rlPendingDelivery;
    @BindView(R.id.rl_pending_evaluated)
    RelativeLayout rl_pendingEvaluated;
    @BindView(R.id.rl_pending_payment)
    RelativeLayout rlPendingPayment;
    @BindView(R.id.rl_pending_receiving)
    RelativeLayout rlPendingReceiving;
    @BindView(R.id.rl_refund)
    RelativeLayout rlRefund;
    @BindView(R.id.rl_collection)
    RelativeLayout rlCollection;
    @BindView(R.id.rl_footprint)
    RelativeLayout rlFootPrint;
    @BindView(R.id.rl_all_order)
    RelativeLayout rlAllOrder;
    @BindView(R.id.rl_balance)
    RelativeLayout rlBalance;
    @BindView(R.id.rl_feedback)
    RelativeLayout rlFeedback;
    @BindView(R.id.ll_mine)
    LinearLayout llMine;
    @BindView(R.id.tv_invitation_code)
    TextView tvInvitationCode;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_mine;
    }

    private static final String TAG = "MineFragment";

    @Override
    protected void initView() {
        if (DecorationApplication.getJustLook()) {
        } else {
        }
        showIsJustLook(DecorationApplication.getJustLook());


        Glide.with(this).load(R.mipmap.background_mine).into(ivUserBackground);
        ScreenUtils.changeLight(ivUserBackground, -50);
        account = Account.getInstance();
//        String headImgLocation = account.getHeadImgLocation();
//        String userSignature = account.getSign();
//        if (headImgLocation != null) {
//            LogUtil.d("mytest", "headimglocation: " + headImgLocation);
//            Glide.with(getActivity()).load(headImgLocation).bitmapTransform(new CropCircleTransformation(getActivity())).placeholder(R.mipmap.default_head_portrait_mine).into(ivUserPortrait);
//        } else {
//            LogUtil.d("mytest", "circle test");
//            Glide.with(getActivity()).load(R.mipmap.default_head_portrait_mine).bitmapTransform(new CropCircleTransformation(getActivity())).into(ivUserPortrait);
//        }
//        if (userSignature != null) {
//            tvUserSignature.setText(userSignature);
//        }
        tvInvitationCode.setText(account.getInviteCode());
    }

    @Override
    public void onResume() {
        LogUtil.i(TAG, "onResume");
        super.onResume();
        String headImgLocation = account.getHeadImgLocation();
        String userSignature = account.getSign();
        // if (headImgLocation != null) {
        LogUtil.d("mytest", "headimglocation: " + headImgLocation);
        ImageUtil.loadCircleImage(getActivity(), headImgLocation, R.mipmap.default_head_portrait_mine, ivUserPortrait);
        //Glide.with(getActivity()).load(headImgLocation).bitmapTransform(new CropCircleTransformation(getActivity())).placeholder(R.mipmap.default_head_portrait_mine).into(ivUserPortrait);
//        } else {
//            LogUtil.d("mytest", "circle test");
//            Glide.with(getActivity()).load(R.mipmap.default_head_portrait_mine).bitmapTransform(new CropCircleTransformation(getActivity())).into(ivUserPortrait);
//        }
        if (userSignature != null) {
            tvUserSignature.setText(userSignature);
        }
    }

    private void showIsJustLook(boolean justLook) {
        if (justLook) {
            llMine.setClickable(true);
            ivSetting.setClickable(false);
            ivNews.setClickable(false);
            rlPendingDelivery.setClickable(false);
            rl_pendingEvaluated.setClickable(false);
            rlPendingPayment.setClickable(false);
            rlPendingReceiving.setClickable(false);
            rlRefund.setClickable(false);
            rlCollection.setClickable(false);
            rlFootPrint.setClickable(false);
            rlAllOrder.setClickable(false);
            rlFeedback.setClickable(false);
        } else {
            llMine.setClickable(false);
            ivSetting.setClickable(true);
            ivNews.setClickable(true);
            rlPendingDelivery.setClickable(true);
            rl_pendingEvaluated.setClickable(true);
            rlPendingPayment.setClickable(true);
            rlPendingReceiving.setClickable(true);
            rlRefund.setClickable(true);
            rlCollection.setClickable(true);
            rlFootPrint.setClickable(true);
            rlAllOrder.setClickable(true);
            rlFeedback.setClickable(true);
        }

    }

    @OnClick({R.id.iv_setting, R.id.iv_news,
            R.id.rl_pending_delivery, R.id.rl_pending_evaluated, R.id.rl_pending_payment, R.id.rl_pending_receiving,
            R.id.rl_refund, R.id.rl_housemaster, R.id.rl_collection, R.id.rl_footprint, R.id.rl_all_order,
            R.id.rl_feedback, R.id.ll_mine, R.id.rl_balance
    })
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_setting:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            case R.id.iv_news:
                startActivity(new Intent(getActivity(), NewsActivity.class));
                break;
            case R.id.rl_all_order:
                startActivity(new Intent(getActivity(), AllOrderActivity.class));
                break;
            case R.id.rl_pending_delivery:
                startActivity(new Intent(getActivity(), PendingDeliveryActivity.class));
                break;
            case R.id.rl_pending_evaluated:
                startActivity(new Intent(getActivity(), PendingEvaluatedActivity.class));
                break;
            case R.id.rl_pending_payment:
                startActivity(new Intent(getActivity(), PendingPaymentActivity.class));
                break;
            case R.id.rl_pending_receiving:
                startActivity(new Intent(getActivity(), PendingReceivingActivity.class));
                break;
            case R.id.rl_refund:
                startActivity(new Intent(getActivity(), RefundActivity.class));
                break;
            case R.id.rl_housemaster:
                startActivity(new Intent(getActivity(), HousemasterActivity.class));
                break;
            case R.id.rl_collection:
                startActivity(new Intent(getActivity(), CollectionActivity.class));
                break;
            case R.id.rl_footprint:
                startActivity(new Intent(getActivity(), FootprintActivity.class));
                break;
            case R.id.rl_feedback:
                startActivity(new Intent(getActivity(), FeedbackActivity.class));
                break;
            case R.id.ll_mine:
                Toast.makeText(getActivity(), "请您先登录", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
            case R.id.rl_balance:
                startActivity(new Intent(getActivity(), BalanceActivity.class));
                break;
            default:
                break;
        }
    }

}
